package com.core.dao.impl;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;
import org.springframework.stereotype.Repository;

import com.core.dao.SekolahDao;
import com.core.entity.Sekolah;

@Repository
public class SekolahDaoImpl extends GenericDaoImpl<Sekolah, Long> implements
		SekolahDao {

	@Override
	protected Class<Sekolah> getEntityClass() {
		return Sekolah.class;
	}

	@Override
	public List<Sekolah> search(Map<String, List<Object>> param) {

		DetachedCriteria criteria = createDetachedCriteria();

		Set<String> keys = param.keySet();
		int valuesCount = 0;
		if (keys.size() > 0) {
			for (String key : keys) {
				
				if ("nama".equals(key)) {
					String value = (String) param.get(key).get(0);
					SimpleExpression nameExp =Restrictions.like(key, value, MatchMode.ANYWHERE);
					nameExp.ignoreCase();
					criteria.add(nameExp);
					valuesCount++;
				} 
				else {

					List<Object> values = param.get(key);
					if (values.size() > 0) {
						criteria.add(Restrictions.in(key, values));
						valuesCount += values.size();
					}
				}
				
				
			}
			if (valuesCount > 0)
				return getHibernateTemplate().findByCriteria(criteria);
			else
				return Collections.EMPTY_LIST;
		} else {
			return Collections.EMPTY_LIST;
		}
	}
}
